﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.WebUserControls.Frame
{

    public partial class AOrganizationServiceFrame : System.Web.UI.UserControl
    {
        [Category("Manipulation")]
        public string HiddenFieldServiceId
        {
            get { return (string)ViewState["HiddenFieldServiceId"] ?? string.Empty; }
            set { ViewState["HiddenFieldServiceId"] = value; }
        }


        [Category("Manipulation")]
        public int ServiceId
        {
            get { return ViewState["ServiceId"] != null ? (int)ViewState["ServiceId"] : -1; }
            set { ViewState["ServiceId"] = value; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            AOrganisationServiceFilter filter = ViewState["filter"] as AOrganisationServiceFilter??new AOrganisationServiceFilter {ServiceId = -1};

            var service = Parent.FindControl(HiddenFieldServiceId) as HiddenField;
            int serviceId;
            if (service != null && int.TryParse(service.Value, out serviceId))
            {
                filter.ServiceId = serviceId;
            }
            else
            {
                filter.ServiceId = ServiceId;
            }

            ViewState["filter"] = filter;

        }

      
        protected void ObjectDataSourceOrganizationService_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            var filter = ViewState["filter"] as AOrganisationServiceFilter;
            e.InputParameters["filter"] = filter;
        }
    }
}